import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:provider/provider.dart';
import 'package:xiongan_app_yy/pages/common/BaseMaterialInfo.dart';
import 'package:xiongan_app_yy/pages/common/LoadBuilder.dart';
import 'package:xiongan_app_yy/pages/common/TextFieldItem.dart';
import 'package:xiongan_app_yy/pages/common/TextItemCard.dart';
import 'package:xiongan_app_yy/provider/autopicking_provider.dart';
import 'package:xiongan_app_yy/provider/libback_provider.dart';
import 'package:xiongan_app_yy/utils/toast_utils.dart';
import 'package:xiongan_app_yy/widgets/comm/left_icon_widget.dart';

class LibBackDetails extends StatefulWidget {
  dynamic data;

  @override
  _LibBackDetailsState createState() => _LibBackDetailsState();

  LibBackDetails(this.data);
}

class _LibBackDetailsState extends State<LibBackDetails> {
  TextEditingController controller0 = TextEditingController();
  TextEditingController controller1 = TextEditingController();
  TextEditingController controller2 = TextEditingController();
  bool downState = true; //按钮显示状态
  bool upState = false;
  bool tfState = false; //编辑框显示状态
  bool makeState = false; //盲样制作按钮状态
  bool downCad = false;
  bool upCad = false;
  Color loadBgColor = Color(0x66BCBCBC);
  bool load = false;

  @override
  void initState() {
    super.initState();
    if (widget.data["orderstate"] == "1") {
      if (widget.data["btnCheckDown"] == 0 && widget.data["btnCheckUp"] == 0) {
        setState(() {
          downState = true;
          upState = true;
          tfState = true;
          makeState = true;
        });
      }
    } else {
      if (widget.data["btnCheckDown"] == 0 && widget.data["btnCheckUp"] == 0) {
        setState(() {
          downState = true;
          upState = true;
          tfState = true;
        });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          leading: LeftIconWidget(icon: Icons.chevron_left, isize: 36),
          backgroundColor: Color(0XFF00A5F1),
          title: Text(
            "样品回库",
            style: TextStyle(fontSize: 18),
          ),
          elevation: 0,
          centerTitle: true,
        ),
        body: LoadBuilder(
            visibility: load,
            bgColor: loadBgColor,
            builder: Column(children: <Widget>[
              Expanded(
                  child: Scrollbar(
                      child: SingleChildScrollView(
                          child: Column(children: <Widget>[
                TextItemCard(
                  name: "回库托盘码",
                  value: widget.data["palletcode"] ?? "暂无",
                ),
                TextItemCard(
                  name: "回库库区",
                  value: widget.data["areaname"] ?? "暂无",
                ),
                TextItemCard(
                  name: "回库货架",
                  value: widget.data["shelfname"] ?? "暂无",
                ),
                TextItemCard(
                  name: "回库货位",
                  value: widget.data["locationname"] ?? "暂无",
                ),
                Column(children: <Widget>[
                  BaseMaterialInfo(
                    imgwid: Image.asset("assets/images/build.png",
                        fit: BoxFit.fill),
                    options: {
                      "materialname": "物资名称",
                      "materialcode": "实物ID",
                      "baseunitname": "基本单位",
                      "quantity": "计划抽样数"
                    },
                    data: widget.data,
                  ),
                  Container(
                    padding: EdgeInsets.all(15),
                    child: Column(
                      children: <Widget>[
                        TextFieldItem(
                            textEditingController: controller0,
                            hintText: "扫描或输入托盘下架位置"),
                        TextFieldItem(
                            textEditingController: controller1,
                            hintText: "扫描或输入托盘存放位置"),
                        TextFieldItem(
                            textEditingController: controller2,
                            hintText: "输入实际回库数"),
                      ],
                    ),
                  )
                ]),
              ])))),
              Container(
                  alignment: Alignment.bottomCenter,
                  width: double.infinity,
                  child: Row(
                    children: <Widget>[
                      Expanded(
                          flex: 1,
                          child: RaisedButton(
                            color: Colors.blue,
                            padding: EdgeInsets.all(12),
                            onPressed: () {
                              if (controller0.text != null &&
                                  controller0.text.length > 0) {
                                widget.data["locationcode"] = controller0.text;
                                if (controller1.text != null &&
                                    controller1.text.length > 0) {
                                  widget.data["newpalletcode"] =
                                      controller1.text;
                                }

                                if (controller2.text != null &&
                                    controller2.text.length > 0) {
                                  widget.data["newlocationcode"] =
                                      controller2.text;
                                }
                                _submit();
                              } else {
                                TostUtil.showToast("请扫描或输入托盘位置");
                              }
                            },
                            child: Text(
                              "回库",
                              style:
                                  TextStyle(color: Colors.white, fontSize: 16),
                            ),
                          ))
                    ],
                  ))
            ])));
  }

  void _submit() {
    Provider.of<LibBackProvider>(context)
        .sendLibBackTask(sendObj: widget.data)
        .then((a) {
      if (a == true) {
        Navigator.pop(context, "ok");
      }
    });
  }
}
